import { defineStore } from 'pinia';

export const usePostStore = defineStore('post', {
    state: () => ({
        allPosts: [],
    }),
    getters: {
        getPostById: (state) => (id) => state.allPosts.find(p => p.id === id),
    },
    actions: {
        setPosts(posts) {
            this.allPosts = posts;
            if (import.meta.env.DEV) {
                console.log('✅ Posts updated in store');
            }
        },
        loadFromCache() {
            const cached = localStorage.getItem('cached_posts');
            if (cached) {
                this.allPosts = JSON.parse(cached);
            }
        }
    }
});
